import { observe } from './observer/index'

export function initState(vm) {
    const opt = vm.$options;
    if(opt.data) {
        initData(vm);
    }
}

function proxy(vm, target, key) {
    Object.defineProperty(vm, key, {
        get() {
            return vm[target][key] 
        },
        set(newValue) {
            vm[target][key] = newValue;
        }
    })
}

function initData(vm) {
    let data = vm.$options.data; //data可能是对象 或者是函数
    data = typeof data === 'function' ? data.call(vm) : data;
    vm._data = data;
    observe(data);

    for (let key in data) {
        proxy(vm, '_data', key)
    }
}